//Copyright (c) 2014 MagicCard
using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace MCChayAnhThuong
{
    public class MChayAnhThuong
    {
        private Vector2 toadoanh, tamxoay;
        private Texture2D anhhienthi, anhcdhienthi, anhkichhoat;
        private int chieurong, chieudai, chisolop, chayhieuung = 0, dochamkichhoat = 5, sodemluot = 0;
        private float tilerong, tiledai, gocxoay;
        private Rectangle bohinhanh;
        private SoundEffect amkichhoat;
        public MChayAnhThuong(int chiso, Texture2D hinhanhthuong, Texture2D hinhanhkichhoat, Vector2 toado, SoundEffect amthanhkichhoat, float tiledanrong, float tiledandai)
        {
            chisolop = chiso;
            tilerong = tiledanrong;
            tiledai = tiledandai;
            NapHinhAnh(hinhanhthuong, hinhanhkichhoat);
            DiChuyen(toado);
            tamxoay = new Vector2(chieurong / 2, chieudai / 2);
            amkichhoat = amthanhkichhoat;
        }
        public Vector2 LayToaDo()
        {
            return toadoanh;
        }
        public Texture2D LayHinhAnh()
        {
            return anhhienthi;
        }
        public int LayChieuRong()
        {
            return chieurong;
        }
        public int LayChieuDai()
        {
            return chieudai;
        }
        public void NapChiSoLop(int chiso)
        {
            chisolop = chiso;
        }
        public void CaiDoChamKichHoat(int docham)
        {
            dochamkichhoat = docham;
        }
        public void NapHinhAnh(Texture2D hinhanhthuong, Texture2D hinhanhkichhoat)
        {
            anhhienthi = hinhanhthuong;
            anhcdhienthi = anhhienthi;
            anhkichhoat = hinhanhkichhoat;
            chieurong = anhhienthi.Width;
            chieudai = anhhienthi.Height;
            DoiKichThuoc(chieurong, chieudai);
        }
        public void DiChuyen(Vector2 toadomoi)
        {
            toadoanh = toadomoi;
            bohinhanh.X = Convert.ToInt32(toadoanh.X * tilerong);
            bohinhanh.Y = Convert.ToInt32(toadoanh.Y * tiledai);
        }
        public void DoiKichThuoc(int rong, int dai)
        {
            if (rong > 0)
            {
                chieurong = rong;
                bohinhanh.Width = Convert.ToInt32(chieurong * tilerong);
            }
            if (dai > 0)
            {
                chieudai = dai;
                bohinhanh.Height = Convert.ToInt32(chieudai * tiledai);
            }
        }
        public void XoayAnh(int goc, Vector2 tam)
        {
            gocxoay = goc * (float)Math.PI / 180;
            tamxoay = tam;
        }
        public void KichHoatNhanh(int chiso, MouseState trohientai, MouseState trotruocdo, string giatricap, ref string giatrilay, float kichthuocamthanh)
        {
            if (chisolop == chiso)
            {
                if (trohientai.LeftButton == ButtonState.Pressed && trotruocdo.LeftButton == ButtonState.Released)
                {
                    //chuyen the toa do kich theo man hinh chuan
                    int toadokichx = Convert.ToInt32(trohientai.X / tilerong);
                    int toadokichy = Convert.ToInt32(trohientai.Y / tiledai);
                    if (KiemTraChua(new Vector2(toadokichx, toadokichy), new Rectangle(Convert.ToInt32(toadoanh.X), Convert.ToInt32(toadoanh.Y), chieurong, chieudai)))
                    {
                        if (amkichhoat != null) amkichhoat.Play(kichthuocamthanh, 0, 0);
                        giatrilay = giatricap;
                    }
                }
            }
        }
        public void KichHoatThuong(int chiso, MouseState trohientai, MouseState trotruocdo, string giatricap, ref string giatrilay, float kichthuocamthanh)
        {
            if (chisolop == chiso)
            {
                if (trohientai.LeftButton == ButtonState.Pressed && trotruocdo.LeftButton == ButtonState.Released)
                {
                    //chuyen the toa do kich theo man hinh chuan
                    int toadokichx = Convert.ToInt32(trohientai.X / tilerong);
                    int toadokichy = Convert.ToInt32(trohientai.Y / tiledai);
                    if (KiemTraChua(new Vector2(toadokichx, toadokichy), new Rectangle(Convert.ToInt32(toadoanh.X), Convert.ToInt32(toadoanh.Y), chieurong, chieudai)))
                    {
                        if (chayhieuung == 0)
                        {
                            if (amkichhoat != null) amkichhoat.Play(kichthuocamthanh, 0, 0);
                            anhhienthi = anhkichhoat;
                            chayhieuung++;
                        }
                    }
                    else
                    {
                        if (chayhieuung != 0)
                        {
                            anhhienthi = anhcdhienthi;
                            chayhieuung = 0;
                        }
                    }
                }
                if (chayhieuung != 0)
                {
                    if (chayhieuung > dochamkichhoat)
                    {
                        anhhienthi = anhcdhienthi;
                        giatrilay = giatricap;
                        chayhieuung = 0;
                    }
                    else chayhieuung++;
                }
            }
        }
        public void KichHoatChon(int chiso, MouseState trohientai, MouseState trotruocdo, string giatricap, ref string giatrilay, float kichthuocamthanh)
        {
            if (chisolop == chiso)
            {
                if (trohientai.LeftButton == ButtonState.Pressed && trotruocdo.LeftButton == ButtonState.Released)
                {
                    //chuyen the toa do kich theo man hinh chuan
                    int toadokichx = Convert.ToInt32(trohientai.X / tilerong);
                    int toadokichy = Convert.ToInt32(trohientai.Y / tiledai);
                    if (KiemTraChua(new Vector2(toadokichx, toadokichy), new Rectangle(Convert.ToInt32(toadoanh.X), Convert.ToInt32(toadoanh.Y), chieurong, chieudai)))
                    {
                        if (amkichhoat != null) amkichhoat.Play(kichthuocamthanh, 0, 0);
                        if (sodemluot < 2) sodemluot++;
                        anhhienthi = anhkichhoat;
                        giatrilay = giatricap + "-" + sodemluot.ToString();
                    }
                    else
                    {
                        anhhienthi = anhcdhienthi;
                        sodemluot = 0;
                    }
                }
            }
        }
        public Boolean DangHoatDong()
        {
            Boolean dhd = false;
            if (chayhieuung != 0) dhd = true;
            return dhd;
        }
        public void HienThiThuong(SpriteBatch nenve)
        {
            if ((DangHoatDong()) | (sodemluot != 0)) nenve.Draw(anhcdhienthi, bohinhanh, Color.White);
            nenve.Draw(anhhienthi, bohinhanh, Color.White);
        }
        public void HienThiXoay(SpriteBatch nenve)
        {
            if ((DangHoatDong()) | (sodemluot != 0)) XoayHinhAnh(nenve, anhcdhienthi, bohinhanh, gocxoay, tamxoay);
            XoayHinhAnh(nenve, anhhienthi, bohinhanh, gocxoay, tamxoay);
        }
        private Boolean KiemTraChua(Vector2 diem, Rectangle vungchua)
        {
            Boolean ketqua = false;
            if (diem.X >= vungchua.X && diem.X <= vungchua.X + vungchua.Width && diem.Y >= vungchua.Y && diem.Y <= vungchua.Y + vungchua.Height)
                ketqua = true;
            return ketqua;
        }
        private void XoayHinhAnh(SpriteBatch nenve, Texture2D hinhanh, Rectangle boanh, float giatrigocxoay, Vector2 toadotamxoay)
        {
            //boanh phai duoc bien dang theo ti le, tam xoay phai tinh theo kich thuoc that cua hinh anh
            boanh.X += Convert.ToInt32(toadotamxoay.X * tilerong);
            boanh.Y += Convert.ToInt32(toadotamxoay.Y * tiledai);
            nenve.Draw(hinhanh, boanh, null, Color.White, giatrigocxoay, toadotamxoay, SpriteEffects.None, 0f);
        }
    }
}